package com.tcreditdemo;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class CardRecyclerAdapter extends RecyclerView.Adapter {

    private static int TITLE_ITEM=0;
    private static int CONTENT_ITEM=1;

    ArrayList<Integer> datas;
    private RecyclerView recyclerView;


    String[] content1=new String[]{"网银","邮箱账单","车险","寿险"};
    String[] content2=new String[]{"运营商","QQ","脉脉"};


    public CardRecyclerAdapter(ArrayList<Integer> datas,RecyclerView recyclerView) {
        this.datas = datas;
        this.recyclerView=recyclerView;
    }

    @Override
    public int getItemViewType(int position) {
       // LogUtil.e("getItemViewType当前位置是："+position);
        if (position==0){
            return TITLE_ITEM;
        }else {
            return CONTENT_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType==TITLE_ITEM){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_title_view, parent, false);
            return new TitleItem(view);
        }else if (viewType==CONTENT_ITEM){
            view=LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_content_view,parent,false);
            return new ContentItem(view);
        }
        return null;
    }



    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof TitleItem){
            TitleItem titleItem=(TitleItem) holder;
         //   LogUtil.e("当前位置是bindholder："+position);
            if (datas.size()==5){
                titleItem.tv.setText("资产信息认证");
            }else if (datas.size()==4){
                titleItem.tv.setText("社交信息认证");
            }else if (datas.size()==2){
                titleItem.tv.setText("身份信息认证");

            }
        }else if (holder instanceof ContentItem){
            ContentItem contentItem = (ContentItem) holder;
            ((ContentItem) holder).settingview.setRightItemOnlyImage();


            if (datas.size()==5){
               // contentItem.tv.setText(content1[position-1]);
                contentItem.settingview.setLeftTitle(content1[position-1]);

            }else if (datas.size()==4){
              //  contentItem.tv.setText(content2[position-1]);
                contentItem.settingview.setLeftTitle(content2[position-1]);
            }else if (datas.size()==2){
               // contentItem.tv.setText("学信网");
                contentItem.settingview.setLeftTitle("学信网");
            }
            if (position == datas.size() - 1) {
                // contentItem.v1.setVisibility(View.GONE);
                contentItem.settingview.getButtomline().setVisibility(View.GONE);
            }

            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager.getClass().getSimpleName().equals(GridLayoutManager.class.getSimpleName())) {

               // contentItem.settingview.setitemHeight();
                contentItem.settingview.setThisHeight(R.dimen.x80);

                //contentItem.settingview.setitemHeight();
               // contentItem.settingview.setThisHeight();
                contentItem.settingview.setRightItemOnlyImage();

                int spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
                    if (position%2!=0&&position==datas.size()-2){
                       // contentItem.v1.setVisibility(View.GONE);
                        contentItem.settingview.getButtomline().setVisibility(View.GONE);
                    }
                    if (position%spanCount==0) {
                      //  contentItem.v2.setVisibility(View.GONE);
                        contentItem.settingview.getRightline().setVisibility(View.GONE);
                    }else {
                       // contentItem.v2.setVisibility(View.VISIBLE);
                        contentItem.settingview.getRightline().setVisibility(View.VISIBLE);
                    }

            }

        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }




    class TitleItem extends RecyclerView.ViewHolder{

        public TextView tv;

        public TitleItem(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);
        }
    }

    class ContentItem extends RecyclerView.ViewHolder{
//        public TextView tv;
//        public View v1;
//        public View v2;
//        public RelativeLayout rl_root;

        public SettingItemView settingview;

        public ContentItem(View itemView) {
            super(itemView);
//            tv = itemView.findViewById(R.id.tv);
//            v1=itemView.findViewById(R.id.v1);
//            v2=itemView.findViewById(R.id.v2);
//            rl_root=itemView.findViewById(R.id.rl_root);
//            if (onItemClickListener != null) {
//                itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        onItemClickListener.onItemClick(v, getLayoutPosition(), tv.getText().toString());
//                    }
//                });
//            }

            settingview = itemView.findViewById(R.id.ry_item);
            if (onItemClickListener != null) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(v, getLayoutPosition(), settingview.getLeftTitle());
                    }
                });
            }


        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position,String title);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
